//
//  GoodsListView.m
//  MindBox
//
//  Created by MS on 16-1-11.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "GoodsListView.h"


#define POPVIEW_HEIGHT 250
#define MENUVIEW_HEIGHT 35
@interface GoodsListView ()<MenuViewDelegate,PopViewDelegate,LGCoverDelegate>

@property (nonatomic,strong) Param * param;
@end

@implementation GoodsListView


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createPopView];
        self.menuView.titleArray = @[@"综合",@"价格",@"热度",@"最新",@"筛选"];
    }
    return self;
}

-(void)createPopView
{
    PopView * popView = [PopView popViewWithXib];
    self.popView = popView;
    popView.delegate = self;
    popView.frame = CGRectMake(0, MENUVIEW_HEIGHT-POPVIEW_HEIGHT, self.width, POPVIEW_HEIGHT);
    [self addSubview:popView];
    
}

-(MenuView *)menuView
{
    if (_menuView==nil) {
        MenuView * view  = [[MenuView alloc] initWithFrame:CGRectMake(0, 0, self.width, MENUVIEW_HEIGHT)];
        [self addSubview:view];
        _menuView = view;
        _menuView.delegate = self;
    }
    return _menuView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.GoodsListViewClickIndexBlock) {
        
        _GoodsListViewClickIndexBlock(indexPath.item);
    }
    
}


-(void)menuViewDidClick:(MenuView *)menuView index:(NSInteger)index type:(BOOL)sortType
{

    if (index==4 && menuView.speBtn.selected==NO) {
        
        LGCover * coverView = [LGCover show];
        coverView.frame = CGRectMake(0, 0, self.width, self.height);
        coverView.backgrondAlph = YES;
        self.coverView = coverView;
        coverView.delegate = self;
        [self insertSubview:coverView belowSubview:_popView];
         _popView.hidden = NO;
        [UIView animateWithDuration:0.25 animations:^{
            _popView.transform = CGAffineTransformMakeTranslation(0, POPVIEW_HEIGHT);
        }];
    }
    
    else {
        
            [UIView animateWithDuration:0.25 animations:^{
                _popView.transform = CGAffineTransformIdentity;
                [_coverView removeFromSuperview];
            } completion:^(BOOL finished) {
                _popView.hidden = YES;
                [_popView resetValue];
            }];

//            [UIView animateWithDuration:0.25 animations:^{
//                _popView.transform = CGAffineTransformIdentity;
//                [_popView resetValue];
//
//
//            }];
        }
    
    
    if (index!=4) {
        Param * pa = [Param param];
        self.param = pa;
        if (index==2 || index==3) {//搜索类型对应的filter_type，热度：3，最新：4
            pa.filter_type = index+1;
        }
        else if (index==1 ){//价格升序：1，降序：2
            pa.filter_type = sortType+1;  //sortType 0升序，1降序
        }
        else if(index==0){//综合
            pa.filter_type = 0;
        }
        
        if ([self.selDelegate respondsToSelector:@selector(goodsListViewDidClickSelectGoods:param:)]) {
            [self.selDelegate goodsListViewDidClickSelectGoods:self param:pa];
             [self.collectionView setContentOffset:CGPointZero animated:YES];
        }
       
    }
  
    
}

-(void)popViewFnishBtnDidClick:(PopView *)popview
{
    self.param.free_postage = popview.freePocks;
    self.param.is_stock = popview.hasGoods;
    self.param.min_price =(int)popview.lowerValue;
    
    if (popview.upValue>1000) {
        self.param.max_price =-1;
    }
    else{
        self.param.max_price =(int)popview.upValue;
    }
    
    
    if ([self.selDelegate respondsToSelector:@selector(goodsListViewDidClickSelectGoods:param:)]) {
        [self.selDelegate goodsListViewDidClickSelectGoods:self param:self.param];
        [self.collectionView setContentOffset:CGPointZero animated:YES];
    }
    
    [self hiddenPopView];
    [_coverView removeFromSuperview];
}

-(void)coverDidClick:(LGCover *)cover
{
    [self hiddenPopView];
    
}


-(void)hiddenPopView
{
    [UIView animateWithDuration:0.25 animations:^{
        _popView.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        _popView.hidden = YES;
        [_popView resetValue];
        _menuView.speBtn.selected = NO;
    }];
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.collectionView.frame = CGRectMake(0, MENUVIEW_HEIGHT, self.width, self.height-MENUVIEW_HEIGHT);
}


@end
